<?php

require_once dirname(__FILE__).'/../lib/detrabGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/detrabGeneratorHelper.class.php';

/**
 * detrab actions.
 *
 * @package    laboratorio
 * @subpackage detrab
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class detrabActions extends autoDetrabActions
{
  public function executeActprecio(sfWebRequest $request){
    $producto = $request->getParameter('pid');
    $prec_prod = Doctrine::getTable('Producto')->find($producto)->getPrecioVta();
    return $this->renderText(json_encode(sprintf("%01.2f", $prec_prod)));
  }
  
  public function executeNew(sfWebRequest $request)
  {
    $detrab = new DetalleTrabajo();
    if($request->hasParameter('tid')){
      $tid = $request->getParameter('tid');
    }else{
      $tid = $this->getUser()->getAttribute('tid');
    }
    $detrab->setTrabajoId($tid);
    $this->form = new DetalleTrabajoForm($detrab);
    $this->detalle_trabajo = $this->form->getObject();
  }
  
  public function executeIndex(sfWebRequest $request){
    if($request->hasParameter('tid')){
      $tid = $request->getParameter('tid');
      $this->setFilters(array("trabajo_id" => $tid));
      $this->getUser()->setAttribute('tid', $tid);
      parent::executeIndex($request);
    }else{
      $this->redirect('trabajo');
    } 
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
      $detalle_resumen = $form->save();
      
      if ($request->hasParameter('_save_and_add')){
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');
        $this->redirect('detrab/new?tid='.$detalle_resumen->getTrabajoId());
      }
      else{
        $this->getUser()->setFlash('notice', $notice);
        $this->redirect('detrab/index?tid='.$detalle_resumen->getTrabajoId());
      }
    }else{
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }  
}
